﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Part3_IndividuleAssignment2.ViewModels;
using System.Net;
using System.Net.Http;
using WebApiContrib.Formatting;

namespace Part3_IndividuleAssignment2.Models
{
    public class NewsSystemRepository
    {
        public List<ViewNews> GetAllViewNewsItems()
        {
            NewsSystemEntities context = new NewsSystemEntities();
            return context.NewsItems.ToList().OrderBy(n => n.PostTime).Select(n => new ViewNews
            {
                Content = n.Content,
                DateAndTime = (n.PostTime).ToString("hh:mmtt MMM dd,yyyy"),
                Region = n.Account.City.CityName + ", " + n.Account.City.Province.ProvinceName
            }).ToList();
        }

        public HttpResponseMessage SubmitNews(SubmitNews submitNews,HttpRequestMessage request)
        {
            NewsSystemEntities context = new NewsSystemEntities();

            Account account = context.Accounts.Where(a => a.Email == submitNews.Email).FirstOrDefault();
            if (null != account)
            {
                NewsItem newsItem = new NewsItem { Account = account, Content = submitNews.Content };
                context.NewsItems.Add(newsItem);
                context.SaveChanges();
                return request.CreateResponse(HttpStatusCode.OK, "Congratulations! News has been successfully submitted.");
            }
            else
            {
                return request.CreateResponse(HttpStatusCode.BadRequest,"Email address has not been registered, please sign up first.");
            }
        }

        public HttpResponseMessage CreateNewAccount(NewAccount newAccount, HttpRequestMessage request)
        {
            NewsSystemEntities context = new NewsSystemEntities();

            if (context.Accounts.Select(a => a.Email).ToList().Contains(newAccount.Email))
                return request.CreateResponse(HttpStatusCode.BadRequest,"Email address has been used. Please try again");

            City city = context.Cities.Where(c => c.CityName == newAccount.City).FirstOrDefault();
            if (null == city)
                return request.CreateResponse(HttpStatusCode.BadRequest, "Invalid request.");

            Account account = new Account
            {
                FirstName = newAccount.FirstName,
                LastName = newAccount.LastName,
                Email = newAccount.Email,
                City = city
            };

            context.Accounts.Add(account);
            context.SaveChanges();
            return request.CreateResponse(HttpStatusCode.OK, "Congratulations! Your account has been successfully created.");
        }

        public List<string> GetAllProvinceNames()
        { 
             NewsSystemEntities context = new NewsSystemEntities();
             return context.Provinces.OrderBy(p => p.ProvinceName).Select(p => p.ProvinceName).ToList();
        }

        public List<string> GetCityNamesByProvinceName(string provinceName)
        {
            NewsSystemEntities context = new NewsSystemEntities();
            return context.Cities.Where(c => c.Province.ProvinceName == provinceName).OrderBy(c=>c.CityName).Select(c => c.CityName).ToList();
        }
    }
}